package com.zx.idc.backend.gui.base.house;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zx.idc.backend.gui.common.BaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.ds.base.house.entity.BaseIdcHouseArea;
import com.zx.idc.ds.base.house.service.IBaseIdcHouseAreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.joor.Reflect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 机房区域信息管理 前端控制器
 * </p>
 *
 * @author rxliuli
 * @since 2019-01-31
 */
@RestController
@RequestMapping("/api/baseIdcHouseArea")
public class BaseIdcHouseAreaController extends BaseApi {

    @Autowired
    private IBaseIdcHouseAreaService iBaseIdcHouseAreaService;

    @GetMapping("/list/{field}/{val}")
    @RequiresPermissions("basic-data:base-house-info")
    public Result<List<BaseIdcHouseArea>> listByHouseId(@PathVariable("field") String field, @PathVariable("val") Long fieldValue) {
        return success(iBaseIdcHouseAreaService.selectList(
            (BaseIdcHouseArea) Reflect.on(BaseIdcHouseArea.class)
                .create()
                .set(field, fieldValue)
                .get()
            )
        );
    }

    @GetMapping("/select-list")
    @RequiresPermissions("basic-data:base-house-info")
    public Result<List<BaseIdcHouseArea>> selectList() {
        return selectList(new BaseIdcHouseArea());
    }

    @PostMapping("/select-list-param")
    @RequiresPermissions("basic-data:base-house-info")
    public Result<List<BaseIdcHouseArea>> selectList(@RequestBody BaseIdcHouseArea baseIdcHouseArea) {
        return success(
            iBaseIdcHouseAreaService.selectList(
                new EntityWrapper<>(baseIdcHouseArea).setSqlSelect("id", "areaName")
            )
        );
    }

    @PostMapping("/delete/{id}")
    @RequiresPermissions("basic-data:base-house-info:edit")
    public Result<Boolean> delete(@PathVariable Long id) {
        return deleteTemplate(() -> iBaseIdcHouseAreaService.deleteArea(id));
    }

}

